package it.uniroma2.art.owlart.langsearch;

import it.uniroma2.art.owlart.langsearch.index.OWLIndexManager;
import it.uniroma2.art.owlart.langsearch.index.config.StandardAnalysisConfiguration;
import it.uniroma2.art.owlart.langsearch.search.OntIndexSearcher;
import it.uniroma2.art.owlart.model.ARTURIResource;
import it.uniroma2.art.owlart.models.OWLModel;
import it.uniroma2.art.owlart.protegeimpl.factory.ARTModelFactoryProtegeImpl;

import java.util.Iterator;

import org.apache.lucene.util.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HERITAGE_ProtegeMEM_TestOld {

	protected static Logger logger = LoggerFactory.getLogger(HERITAGE_ProtegeMEM_TestOld.class);

	public static void main(String[] args) {
		try {
			// G:/WORK/PROJECTS/CUSPIS/Ontologia/0.3
			String ontfile;
			if (args.length == 0) {
				System.out.println("please specificy ontology file");
				ontfile = "heritage.owl";
				// System.exit(0);
			} else
				ontfile = args[1];

			double threshold;

			ARTModelFactoryProtegeImpl fact = new ARTModelFactoryProtegeImpl();

			// this ontology is different from the original one, in that concept Accomodation has the
			// "it" label "Alloggio" marked wrongly in UPPER CASE ("IT"),
			// to test if the indexer is completely CASE INSENSITIVE
			OWLModel model = fact.loadOWLModel("http://art.uniroma2.it/ontologies/heritage",
					"./data/ontologies", ontfile);

			String indexLocation = "./data/index";

			StandardAnalysisConfiguration conf = new StandardAnalysisConfiguration(Version.LUCENE_36, "en", "it");
			conf.setLanguagesOfURILocalNames("en", "it");
			OWLSearchEngine oi = new OWLSearchEngine(model, indexLocation, conf);

			oi.initializeStandardIndexWriters();

			System.out.println("building index at: " + indexLocation);
			oi.buildIndex(true);
			logger.info("INDEX BUILT");

			oi.createOntIndexSearcher();
			logger.info("ontsearcher armed");

			Iterator<ARTURIResource> retrievedResources;

			// example over CUSPIS heritage.owl ontology

			retrievedResources = oi.findClasses("restaurant", "en", OntIndexSearcher.FUZZY_MATCH)
					.iterator();

			while (retrievedResources.hasNext())
				System.out.println(retrievedResources.next());

			retrievedResources = oi.findIndividuals("biblioteca greca", "it",
					OntIndexSearcher.FUZZY_MATCH).iterator();

			while (retrievedResources.hasNext())
				System.out.println(retrievedResources.next());

			retrievedResources = oi.findClasses("Athletics", "en", OntIndexSearcher.FUZZY_MATCH)
					.iterator();
			while (retrievedResources.hasNext())
				System.out.println(retrievedResources.next());

			System.out.println("*******************************************\n" +
					"************* TESTING UPDATES *************\n" +
					"*******************************************");
			
			
			OWLIndexManager idxMgr = oi.getIndexManager();
			ARTURIResource newCls = model.createURIResource("http://pippo.it#newCls");
			model.addLabel(newCls, "new Class", "en");
			model.addLabel(newCls, "nuova classe", "it");

			idxMgr.addNewClassToIndex(newCls);
			idxMgr.updateLangIndexForClass(newCls, "it");
			idxMgr.updateLangIndexForClass(newCls, "en");

			retrievedResources = oi.findClasses("classe", "it", OntIndexSearcher.FUZZY_MATCH)
					.iterator();
			while (retrievedResources.hasNext())
				System.out.println(retrievedResources.next());

			logger.debug("deleting: " + newCls + " from indexes");
			idxMgr.deleteClassFromIndexes(newCls);

			retrievedResources = oi.findClasses("classe", "it", OntIndexSearcher.FUZZY_MATCH)
					.iterator();
			while (retrievedResources.hasNext())
				System.out.println(retrievedResources.next());

			/*
			 * threshold = 0.2; System.out.println("setting threshold to: " + threshold);
			 * oi.setTHRESHOLD(threshold);
			 * 
			 * 
			 * retrievedResources = oi.findProperties("razio", "it", OntIndexSearcher.FUZZY_MATCH)
			 * .iterator();
			 * 
			 * while (retrievedResources.hasNext()) System.out.println(retrievedResources.next());
			 */

		} catch (Exception e) {
			
			System.err.println(e.getMessage());
			System.err.flush();
			
			e.printStackTrace(System.err);
			logger.error(e.getMessage());


			
			e.printStackTrace();
		}
	}

}
